﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Collections;
using System.Windows.Data;
using System.Windows;

namespace DocManagerControls.CLASSI
{
   public class GridColumnsGenerator
    {
       public GridColumnsGenerator()
       {
       }


       public DataGridComboBoxColumn getComboBoxColumn(string pHeaderCol,string pPathDaMostrareInCombo,string pValuePathPerLaCombo,
           string pCampoTabellaDaAssociare,IEnumerable pdatiDaBindareInCombo)
       {
           DataGridComboBoxColumn comboCol = new DataGridComboBoxColumn()
           {               
               DisplayMemberPath = pPathDaMostrareInCombo, //"Categoria", // relativa alla combo
               SelectedValuePath = pValuePathPerLaCombo,//"ID",
               Header = pHeaderCol,//"Categorie",
               ItemsSource = pdatiDaBindareInCombo,// listCategorie,
               IsReadOnly = false,
               SelectedValueBinding = new Binding(pCampoTabellaDaAssociare)
               {
                   NotifyOnValidationError = true,
                   ValidatesOnDataErrors = true,
                   ValidatesOnExceptions = true
               }
               //Binding("Fk_IDCat"),
               //SortMemberPath = "Fk_IDCat",
               //TextBinding = new Binding("ID")    

           };
                      

           return comboCol;

       }//fM


       public DataGridTextColumn getTextBoxColumn(string pHeaderCol, string pCampoTabellaDaAssociare)
       {
           DataGridTextColumn col = new DataGridTextColumn()
           {
               Header = pHeaderCol  ,
               Binding = new Binding(pCampoTabellaDaAssociare)
               {
                   NotifyOnValidationError = true,
                   ValidatesOnDataErrors = true,
                   ValidatesOnExceptions = true,
                   UpdateSourceTrigger= UpdateSourceTrigger.PropertyChanged 
                    //ValidationRules = new System.Collections.ObjectModel.Collection<ValidationRule>
                    //   (
                    //    new List<ValidationRule>(){  new ExceptionValidationRule()}
                    //   )
               }
           };

           //Binding binding = BindingOperations..GetBinding(col, DataGridTextColumn..TextProperty);
           //binding.ValidationRules.Clear();
           //binding.ValidationRules.Add(myCrazyValidationRule);

           return col;

       }//fM


       public DataGridTemplateColumn getDateTimeColumn(string pHeaderCol, string pCampoTabellaDaAssociare)
       {
           var col = new DataGridTemplateColumn  ()
           {
               Header = pHeaderCol ,
               
           };
           // Create a factory. This will create the controls in each cell of this
           // column as needed.
           FrameworkElementFactory factoryData =new FrameworkElementFactory(typeof(DatePicker));

           FrameworkElementFactory factoryText =new FrameworkElementFactory(typeof(Label));
                      
           // Bind the value of this cell to the value of the Date property of the
           // DataContext of this row. The StringFormat "d" will be used to display
           // the value.
           Binding b = new Binding(pCampoTabellaDaAssociare) {
               StringFormat = "{dd/MM/yyyy}",
               NotifyOnValidationError = true,
               ValidatesOnDataErrors = true,
               ValidatesOnExceptions = true,
               UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged           
           };

           factoryData.SetValue(DatePicker.SelectedDateProperty, b);
           factoryText.SetValue(Label.ContentProperty, b);
           factoryText.SetValue(Label.ContentStringFormatProperty ,"dd-MM-yyyy");  

           // Create the template itself, and add the factory to it.
           DataTemplate cellEditingTemplate = new DataTemplate(){ VisualTree = factoryData};
           DataTemplate cellNormalTemplate = new DataTemplate(){ VisualTree = factoryText};
           


           col.CellEditingTemplate = cellEditingTemplate;
           col.CellTemplate = cellNormalTemplate;
           
           return col;

       }//fM

       public DataGridCheckBoxColumn getCheckBoxColumn(string pHeaderCol, string pCampoTabellaDaAssociare)
       {
           var col = new DataGridCheckBoxColumn()
           {
               Header = pHeaderCol,
               Binding = new Binding(pCampoTabellaDaAssociare)
               {
                   NotifyOnValidationError = true,
                   ValidatesOnDataErrors = true,
                   ValidatesOnExceptions = true,
                   UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged                 
               }
           };

           return col;

       }//fM


    }//fC
}
